/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
#ifndef GAUTOSERVER_H
#define GAUTOSERVER_H

// qt
#include <QObject>
#include <QHostAddress>
#include <QTcpServer>
class QTcpSocket;

// local
class GAutoPacket;
class GAutoRequest;
class GAutoReplyFrame;
class GAutoXmlConfig;
class GAutoReplyXmlConfig;
class GAutoMarkers;
class GAutoRoi;
class GAutoParams;




// =================================
// constants
#define XML_CONFIG_FILE "config.xml"
#define MARKERS_FILE	"markers.mask"
#define ROI_FILE		"roi.mask"
#define PARAMS_FILE     "params.dat"




/// The ICud network servers. Serves serialized requests via network conection
/// and performs operations on ICud implementation.
/// ICud implementation should be created outside this project and provided in
/// Constructor or with setCud(),
/// netowrk socket may be created outside, or via build in server method listen()
/// After configuration call processPackets to starts processing loop
class GAutoServer : public QObject
{
	Q_OBJECT
public:

	// construction / destruction

	explicit GAutoServer( QObject* pParent = NULL );
	virtual ~GAutoServer();

	// network
	
	void setSocket( QTcpSocket* pSocket );			///< Set socket
	/// listen for incoming connection
	void listen( quint16 port, const QHostAddress & address = QHostAddress::Any );
	bool isConnected() const;						///< check conection state
	void disconnectFromHost();						///< break connection

	// packet processing
	void processPackets();							///< Packet processing loop
	
	/// Gets next frame from camera and send it to client.
	void processRequestFrame( GAutoReplyFrame * pReplyFrame );
	
	/// Save xml config file.
	void processXmlConfig( GAutoXmlConfig * pXmlConfig );
	
	/// Sends back xml config file. If there isn't any sends empty file.
	void processRequestXmlConfig( GAutoReplyXmlConfig * pReplyXmlConfig );
	
	/// Saves markers analyzer image.
	void processMarkers( GAutoMarkers * pMarkers );
	
	/// Saves roi analyzer image.
	void processRoi( GAutoRoi * pRoi );
	
	/// Save params to params data base.
	void processParams( GAutoParams * pParams );

private:

	QTcpSocket* _pSocket;	///< comm socket
	QTcpServer	_server;	///<server
	int			_timeout;	///< aloowable timeout [ms]
	
	/// sends packet
	void sendPacket( const GAutoPacket& packet );
	
	/// Waits for packet
	GAutoPacket * waitForPacket();

	/// Process incoming packet
	void processPacket( GAutoPacket * pPacket );
	/// Process request packet
	void processRequest( GAutoRequest * pPacket );
};

#endif	// GAUTOSERVER_H




// eof
